package com.yd.app.mapper.provider;

import com.yd.domain.searchParam.RoleParam;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author zy
 * @create 2018-03-07 15:30
 * @des 角色管理
 **/
public class RoleProvider  extends BaseProvider {
    /**
     * @Author: zy
     * @Desc: 分页查询角色数据
     * @Date:  2018/3/7
     */
    public String searchRoleList(Map map){
        RoleParam RoleParam= (RoleParam) map.get("roleParam");
        StringBuilder sb=new StringBuilder("select r.code,r.title,r.des from sys_role r " +
                "WHERE 1=1 ");
        sb.append(createSearchSanWhereSql(RoleParam));
        sb.append("  limit #{startRow},#{pageSize}");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:查询数据
     * @Date:  2018/3/7
     */
    public String searchRoleCount(Map map){
        RoleParam roleParam= (RoleParam) map.get("roleParam");
        StringBuilder sb=new StringBuilder("SELECT COUNT(1) from sys_role r  " +
                "  where 1=1 ");
        sb.append(createSearchSanWhereSql(roleParam));
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:角色查询条件
     * @Date:  2018/3/7
     */
    private String createSearchSanWhereSql(RoleParam roleParam){
        StringBuilder sb=new StringBuilder();
        if(!StringUtils.isEmpty(roleParam.getTitle())){
            sb.append(" and r.title like  '%"+roleParam.getTitle()+"%'  ");
        }
        if(!StringUtils.isEmpty(roleParam.getCode())){
            sb.append(" and r.code = '"+roleParam.getCode()+"'  ");
        }
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc: 获取角色拥有的权限数据
     * @Date:  2018/3/7
     */
    public String searchPermissionList(Map map){
        StringBuilder sb=new StringBuilder("select sp.* from sys_role_perm srp left join sys_perm sp on srp.permCode=sp.`code`" +
                " where srp.roleId=#{roleId} ");
        sb.append("  limit #{startRow},#{pageSize} ");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:查询角色拥有权限条数
     * @Date:  2018/3/7
     */
    public String searchPermissionCount(Map map){
        StringBuilder sb=new StringBuilder(" select count(1) from sys_role_perm srp " +
                " where srp.roleId=#{roleId} ");
        return sb.toString();
    }
   /**
    * @Author: zy
    * @Desc: 分页获取角色尚未拥有权限列表数据
    * @Date:  2018/3/7
    */
    public String searchNotHavePermissionList(Map map){
        StringBuilder sb=new StringBuilder("select * from sys_perm p where p.`code` not IN " +
                "(SELECT r_p.permCode from sys_role_perm r_p  WHERE r_p.roleId=#{roleId})");
        sb.append("  limit #{startRow},#{pageSize} ");
        return sb.toString();
    }

    /**
     * 角色未有权限总条数
     * @param map
     * @return
     */
    public String searchNotHavePermissionCount(Map map){
        StringBuilder sb=new StringBuilder("select count(1) from sys_perm p where p.`code` not IN " +
                "(SELECT r_p.permCode from sys_role_perm r_p  WHERE r_p.roleId=#{roleId})");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:批量添加角色权限
     * @Date:  2018/3/7
     */
    public String insertPermissionRole(Map map){
        List list= (List) map.get("list");
        StringBuilder sb=new StringBuilder("insert into sys_role_perm (roleId,permCode) values");
        sb.append(createBatchAddValuesSql("list",list,"roleId","permCode"));
        sb.append(" ON DUPLICATE KEY UPDATE `roleId`=values(roleId)");
        return sb.toString();
    }

}
